<?php
/**
 * Created by PhpStorm.
 * User: xiaoxin
 * Date: 2018/10/25 0025
 * Time: 13:45
 */
include './functions.php';
include './Db.php';
session_start();
$post = $_POST;
if ($post['captcha']!=$_SESSION['code']){
	exit(json_encode(['code'=>1,'msg'=>'验证码错误']));
}
$preg_username = "/^[\w]{4,10}$/";
$yes1 = preg_match($preg_username,$post['username']);
if (!$yes1){
	exit(json_encode(['code'=>1,'msg'=>'昵称不符合要求']));
}
$preg_phone = "/^1[0-9]{10}$/";
$yes2 = preg_match($preg_phone,$post['phone']);
if (!$yes2){
	exit(json_encode(['code'=>1,'msg'=>'手机格式不符合要求']));
}
$preg_password = "/^[\w@]{6,20}$/";
$yes3 = preg_match($preg_password,$post['password']);
if (!$yes3){
	exit(json_encode(['code'=>1,'msg'=>'密码不符合要求']));
}
if ($post['password']!=$post['repassword']){
	exit(json_encode(['code'=>1,'msg'=>'两次密码不一致']));
}
$db = new Db('user');
$db->where("username='".$post['username']);
$user = $db->select();
if ($user){
	exit(json_encode(['code'=>1,'msg'=>'该用户已存在']));
}
$db->where("phone=".$post['phone']);
$phone = $db->select();
if ($phone){
	exit(json_encode(['code'=>1,'msg'=>'该手机已被注册']));
}

$db->where("email=".$post['email']);
$email = $db->select();
if ($email){
	exit(json_encode(['code'=>1,'msg'=>'该邮箱已被注册']));
}
$post['create_time'] = time();
$post['password'] = md5($post['password']);
unset($post['repassword']);
unset($post['captcha']);
$res = $db->save($post);
if ($res){
	exit(json_encode(['code'=>0,'msg'=>'注册成功']));
}else{
	exit(json_encode(['code'=>1,'msg'=>'注册失败，请稍后再试']));
}
